clear

qui forvalues n = 1974/2017 {
use "microdata/demographicdata/fesdemo`n'.dta", clear
keep hhref persno region numads numhhkid ifshhtyp numpeeps agehd marstat ageced empstat occ penflag firinhh firinbu hhweight hhgnumpeep buno numtuad numtupeeps grosfac	gnumpeep	gnumkid region emppos educ

keep if firinbu == 1 
gen weight_tmp = round(grosfac*1000, 1)
bysort hhref: egen weight_pre = mean(weight_tmp)
drop weight_tmp
gen np = round(hhgnumpeep*1000,1)
gen weight = round(hhweight*1000, 1)
keep if firinhh == 1 
duplicates drop hhref, force
save processed/demosave/demo`n'.dta,replace
}

qui forvalues n = 1974/2017 {
use "microdata/rpiexpendituredata/rpicons`n'.dta", clear
keep hhref caseno datayear year month week totexp FOOD CATERING ALCOHOL TOBACCO HOUSING FUEL_LIG HHGOODS HHSERVS CLOTH_SH P_GD_SER MOTORING FARESET LGOODS LSERVS nondur semidur durables mortgagetot mortgageint insgr bread	cereals	biscuits beef	lamb	pork	bacon	poul_oth poultry	oth_meat	fish butter		cheese	eggs	milkfres	milkprod oil_fats fruit potatoes	oth_vegs swe_choc sug_pres oth_food tea	coffee	softdrin offwinsp	offbeer onbeer onwinsp  domservs petcare postage	telephon	telemess  fees_sub 

merge m:1 hhref using "processed/demosave/demo`n'.dta",keep(match) nogen

save processed/rpisave/rpi`n'.dta,replace
}


use "processed/rpisave/kidrpi1995.dta", clear
forvalues n = 1996/2017 {
append using processed/rpisave/kidrpi`n'.dta
}
save processed/rpisave/kidrpi_master.dta,replace

qui forvalues n = 1974/1994 {
use "processed/rpisave/rpi`n'.dta", clear
	xtile quant200 = totexp, nq(200) 	
	xtile quant100 = totexp, nq(100) 	
	xtile quant50 = totexp , nq(50) 
	xtile quant20 = totexp , nq(20) 
	xtile quant = totexp , nq(10) 
	xtile quint = totexp , nq(5)
	pctile pp_value = totexp , nq(10) genp(percent)
save "processed/rpisave/rpisum`n'.dta",replace
}

qui forvalues n = 1995/2017 {
use "processed/rpisave/rpi`n'.dta", clear
merge m:1 datayear caseno using processed/rpisave/kidrpi`n'.dta
drop _merge

foreach x of varlist totexp FOOD CATERING ALCOHOL TOBACCO HOUSING FUEL_LIG HHGOODS HHSERVS CLOTH_SH P_GD_SER MOTORING FARESET LGOODS LSERVS nondur semidur durables mortgagetot mortgageint insgr bread	cereals	biscuits beef	lamb	pork	bacon	poul_oth poultry	oth_meat	fish butter		cheese	eggs	milkfres	milkprod oil_fats fruit potatoes	oth_vegs swe_choc sug_pres oth_food tea	coffee	softdrin offwinsp	offbeer onbeer onwinsp  domservs petcare postage	telephon	telemess  fees_sub 	 {
replace `x' = `x' + kid`x' if kid`x' != .
drop kid`x'
}
	xtile quant200 = totexp, nq(200) 	
	xtile quant100 = totexp, nq(100) 	
	xtile quant50 = totexp , nq(50) 
	xtile quant20 = totexp , nq(20) 
	xtile quant = totexp , nq(10) 
	xtile quint = totexp , nq(5)
	pctile pp_value = totexp , nq(10) genp(percent)	

if `n' > 1996 {
	drop quant* quint pp_value percent
	xtile quant200 = totexp [pweight=weight], nq(200) 	
	xtile quant100 = totexp [pweight=weight], nq(100) 	
	xtile quant50 = totexp [pweight=weight], nq(50) 
	xtile quant20 = totexp [pweight=weight], nq(20) 
	xtile quant = totexp [pweight=weight], nq(10) 
	xtile quint = totexp [pweight=weight], nq(5)
	pctile pp_value = totexp [pweight=weight], nq(10) genp(percent)
}

save "processed/rpisave/rpisum`n'.dta",replace
}


clear
use "processed/rpisave/rpisum1974.dta", clear
qui forvalues n = 1975/2017 {
append using "processed/rpisave/rpisum`n'.dta" 
}
**RPI merge
/*
gen sum1 = FOOD + CATERING + ALCOHOL + TOBACCO + CLOTH_SH + HOUSING- mortgageint + FUEL_LIG  +HHGOODS +P_GD_SER + HHSERVS +MOTORING + FARESET + LGOODS + LSERVS 
gen sum2 = FOOD + CATERING + ALCOHOL
gen sumFood = bread	+ cereals	+biscuits+	beef+	lamb+	pork+	bacon+	poul_oth + poultry+	oth_meat+	fish+	butter+	oil_fats+	cheese+	eggs+	milkfres+	milkprod+	tea+	coffee+	softdrin+	sug_pres+	swe_choc+	potatoes+	oth_vegs+	fruit+	oth_food
gen FOOD2 = FOOD - sumFood
*/
*1. BREADCEREAL 
gen erpi1 = bread+ cereals + biscuits

*2. Meat_Fish 
gen erpi2 = beef+ lamb+pork+bacon+poul_oth +poultry+oth_meat+fish

*3. Milk_Eggs 
gen erpi3 = butter+ cheese +eggs + milkfres +milkprod

*4 Oilfats
gen erpi4 = oil_fats

*5 Fruit 
gen erpi5 =fruit

*6 Vegetables
gen erpi6 = potatoes + oth_vegs

*7 otherfood
gen erpi7 = swe_choc + sug_pres + oth_food

*8 SOFTDRINK
gen erpi8 = tea +  coffee + softdrin
*gen sumFood =  FOOD
gen sumFooddis = erpi1 + erpi2 + erpi3 + erpi4 + erpi5 + erpi6 + erpi7 + erpi8
* 9   TOBACCO
*gen erpi9 = offwinsp + offbeer + TOBACCO
gen erpi9 = TOBACCO

* 10 CATERING + ALCOHOL
gen erpi10 = ALCOHOL + CATERING
*gen erpi10 = onbeer + onwinsp + CATERING
*gen sum2dis = erpi1 + erpi2 + erpi3 + erpi4 + erpi5 + erpi6 + erpi7 + erpi8 + erpi9 + erpi10
* 11 FUEL_LIG 
*gen erpi11 = FUEL_LIG

* 11 Housing
gen erpi11 = HOUSING + FUEL_LIG - mortgageint - insgr

* 12 Clothing
gen erpi12 = CLOTH_SH

* 13 HHGS
*gen erpi14 = HHGOODS + domservs - petcare
gen erpi13 = HHGOODS + domservs 

* 14. Communication: 
gen erpi14 = postage + telephon + telemess
* 15 PG_S
gen erpi15 = P_GD_SER + fees_sub + insgr
* 16 Transport
gen erpi16 = MOTORING + FARESET
* 17 LGS:
*gen erpi18 = LGOODS + LSERVS + petcare
gen erpi17 = LGOODS + LSERVS 
gen Irpi = totexp - mortgageint
gen I =  totexp
drop if I < 0

** micro **
preserve
forvalues x = 1/17	 {
gen s_RPI_`x' = erpi`x'/Irpi
}
replace weight = weight_pre if year <1997
keep s_RPI_* Irpi I datayear caseno quant	quint	pp_value	percent weight penflag agehd marstat agece numhhkid ifshhtyp numpeeps region emppos educ occ
save processed/RPImicro,replace
restore
replace weight = weight_pre if year <1997
**Representative Aggregation*****
bysort datayear: egen double I_REP = sum(I)
bysort datayear: egen double I_REPrpi = sum(Irpi)
bysort datayear: egen double num_hh = count(weight)
bysort datayear: egen double I_REPw = sum(Irpi*weight)
bysort datayear: egen double num_hhw = sum(weight)
bysort datayear: egen double num_hhw_pre = sum(weight_pre)
bysort datayear: egen double I_REPw_pre = sum(Irpi*weight_pre)
bysort datayear: egen double ave_numpeeps = mean(numpeeps)


format I_REPw I_REPw_pre %12.0f

forvalues x = 1/17	 {
bysort datayear: egen double  erpi`x'_REP = total(erpi`x'*weight)
gen s_RPI_`x'_REP = erpi`x'_REP/I_REPw
format erpi`x'_REP %12.0f
}
gen PC_per = I_REP/num_hh
replace  PC_per = I_REPw/num_hhw if year >1996
duplicates drop datayear ,force
keep s_RPI_* PC_per datayear quant	quint	pp_value	percent

save processed/RepresentativeRPI,replace